package com.djx.zookepper连接集群;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Test;

import java.io.IOException;

public class ZookeeperConnection {
    String whichPerformed = "192.168.15.132:2181,192.168.15.132:2182,192.168.15.132:2183";
    int timeOut = 9000;
    ZooKeeper zooKeeper;

    //zookeeper集群连接
    public void ZkConnection() {
        try {
            zooKeeper = new ZooKeeper(whichPerformed, timeOut, new Watcher() {
                public void process(WatchedEvent watchedEvent) {
                    if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                        System.out.println("zookeeper连接成功!!!");
                    }
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test01() {
        try {
            ZkConnection();
            System.out.println(new String(zooKeeper.getData("/aaa", false, null)));
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
